import { createConnection } from "./dbutils.js";

const DEMAND_SQL_1 = "SELECT \
a.device_id,\
b.device_name,\
a.mete_id,\
c.mete_name,\
a.val,\
a.change_val,\
a.recordtime \
FROM\
usmschis.t_his_ems_metedatahis_202301 a\
JOIN usmsc.t_cfg_device b \
ON a.device_id = b.device_id AND b.device_id = ?\
JOIN usmsc.t_cfg_telemeter c \
ON a.device_id = c.device_id AND a.mete_id = c.mete_id AND c.mete_id = ? \
WHERE a.recordtime BETWEEN ? AND ? \
ORDER BY\
a.recordtime ASC"

function queryData(deviceId, meteId, startDate, endDate) {
    let connection = createConnection();
    connection.connect();
    connection.query(DEMAND_SQL_1,[deviceId, meteId, startDate, endDate] ,(err, res) => {
        if (err == null) {
            success(res);
        } else {
            throw new Error(err);
        }
    });
    connection.end();
}

export {queryData};